package com.nxk.modules.core.web;

import com.nxk.modules.core.entity.SysRole;
import com.nxk.modules.core.service.SysMenuRoleService;
import com.nxk.modules.core.service.SysRoleService;
import com.nxk.modules.exception.ResultEntity;
import com.nxk.modules.utils.ListToJson;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/a/core/menuRole")
public class SysMenuRoleController {

    @Autowired
    private SysMenuRoleService sysMenuRoleService;

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 保存数据
     * @param menuIds
     * @param roleId
     * @return
     */
    @PutMapping("/save")
    @RequiresPermissions({"core:menuRole:edit"})
    public ResultEntity save(String menuIds, String falsePid,String roleId) {
        SysRole role = sysRoleService.getById(roleId);
        if ("system".equals(role.getRoleCode())){
            return ResultEntity.fail("超级管理员权限不可更改!");
        }
        List<String> list = Arrays.asList(menuIds.split(","));
        List<String> falseList = Arrays.asList(falsePid.split(","));
        boolean flag = sysMenuRoleService.save(list, falseList,roleId);
        return flag ? ResultEntity.success() : ResultEntity.fail();
    }


    /**
     * 根据角色id,查看角色拥有的菜单权限
     *
     * @param roleId
     * @return
     */
    @PostMapping("/list")
    public ResultEntity list(String roleId) {
        return ResultEntity.success(sysMenuRoleService.queryMenuByRoleId(roleId));
    }

    /**
     * 根据角色编码,查看角色已经选择(勾选)菜单权限
     *
     * @param roleCode
     * @return
     */
    @PostMapping("/list2")
    public ResultEntity list2(String roleCode) {
        return ResultEntity.success(sysMenuRoleService.queryMenuByRoleCode(roleCode));
    }


    /**
     * 根据角色编码,查看角色拥有的菜单权限
     *
     * @param roleCode
     * @return
     */
    @PostMapping("/list3")
    public ResultEntity list3(String roleCode) {
        return ResultEntity.success(ListToJson.getMenuJson3(sysMenuRoleService.queryMenuByRoleCode(roleCode)));
    }





}
